package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
        implements RoleService {

    @Autowired
    private RoleService roleService;
    @Autowired
    private AdminRoleService adminRoleService;

    //条件分页列表查询
    @Override
    public IPage<Role> selectPage(Page<Role> pageParam,
                                  RoleQueryVo roleQueryVo) {
        //获取条件值：角色名称
        String roleName = roleQueryVo.getRoleName();
        //创建条件构造器对象
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        //判断条件值是否为空
        if (!StringUtils.isEmpty(roleName)) {
            //封装条件
            wrapper.like(Role::getRoleName, roleName);
        }
        //调用mapper方法实现条件分页查询
        IPage<Role> pageModel = baseMapper.selectPage(pageParam, wrapper);
        return pageModel;
    }

    //根据用户id查询所有角色，返回角色值集合
    @Override
    public Map<String, Object> findRoleByUserId(Long adminId) {
        //1、查询所有的角色
        List<Role> allRolesList = baseMapper.selectList(null);

        //2、根据拥有的角色id，获取用户已分配的角色id列表
        //2.1 根据用户查询  用户角色关系表  admin_role  查询用户分配角色id列表
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);
        List<AdminRole> adminRoleList = adminRoleService.list(wrapper);
        //另外一种方式
        /*List<AdminRole> adminRoleList = adminRoleService.list(
                new QueryWrapper<AdminRole>().eq("admin_id", adminId).select("role_id"));*/

        //2.2 通过第一步返回返回集合，获取所有角色id的列表List<Long>
        List<Long> roleIdList = new  ArrayList<>();
        for (AdminRole adminRole : adminRoleList) {
            Long roleId = adminRole.getRoleId();
            roleIdList.add(roleId);
        }
        //另外一种方式
        /*List<Long> roleIdList = adminRoleList.stream()
                .map(c -> c.getRoleId())
                .collect(Collectors.toList());*/

        //2.3 创建新的list集合，用于封装已分配角色
        List<Role> assignRoles = new ArrayList<Role>();
        //2.4 遍历所有角色列表allRolesList，得到每个角色id
        //判断当前角色id，是否包含在已分配角色id列表中，封装到2.3里面新的list集合
        for (Role role : allRolesList) {
            //已分配
            if (roleIdList.contains(role.getId())) {
                assignRoles.add(role);
            }
        }
        //创建返回的Map，封装到map集合中
        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("allRolesList", allRolesList);//所有角色
        roleMap.put("assignRoles", assignRoles);//用户分配角色id集合
        return roleMap;
    }

    @Override
    public void saveUserRoleRealtionShip(Long adminId, Long[] roleIds) {
        //删除用户分配的角色数据
        QueryWrapper<AdminRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", adminId);
        adminRoleService.remove(queryWrapper);
//        adminRoleService.remove(new QueryWrapper<AdminRole>().eq("admin_id", adminId));

        //分配新的角色
        //遍历所有角色id，得到每个角色id，拿着每个角色id+用户id添加到adminRole用户角色表中
        List<AdminRole> userRoleList = new ArrayList<>();
        for ( Long roleId : roleIds) {
            if (StringUtils.isEmpty(roleId)) continue;
            AdminRole userRole = new AdminRole();
            userRole.setAdminId(adminId);
            userRole.setRoleId(roleId);
            userRoleList.add(userRole);
        }
        adminRoleService.saveBatch(userRoleList);
    }
}
